import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../services/auth_service.dart';
import '../models/user.dart';
import 'admin_panel_screen.dart';

class ProfileScreen extends StatelessWidget {
  const ProfileScreen({super.key});

  @override
  Widget build(BuildContext context) {
    final user = context.watch<AuthService>().user;
    final isAdmin = user?.email == '123455';

    return Scaffold(
      appBar: AppBar(
        title: const Text('个人中心'),
        actions: [
          IconButton(
            icon: const Icon(Icons.logout),
            onPressed: () => context.read<AuthService>().logout(),
          ),
        ],
      ),
      body: ListView(
        padding: const EdgeInsets.all(16),
        children: [
          _buildUserInfo(user),
          const SizedBox(height: 16),
          _buildMenuItems(context, isAdmin),
        ],
      ),
    );
  }

  Widget _buildUserInfo(User? user) {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Text(
              '个人信息',
              style: TextStyle(
                fontSize: 20,
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(height: 16),
            ListTile(
              leading: const CircleAvatar(
                child: Icon(Icons.person),
              ),
              title: Text(user?.username ?? '未登录'),
              subtitle: Text(user?.email ?? ''),
            ),
            if (user?.phone != null)
              ListTile(
                leading: const Icon(Icons.phone),
                title: const Text('手机号码'),
                subtitle: Text(user!.phone!),
              ),
            if (user?.address != null)
              ListTile(
                leading: const Icon(Icons.location_on),
                title: const Text('地址'),
                subtitle: Text(user!.address!),
              ),
          ],
        ),
      ),
    );
  }

  Widget _buildMenuItems(BuildContext context, bool isAdmin) {
    return Card(
      child: Column(
        children: [
          ListTile(
            leading: const Icon(Icons.person_outline),
            title: const Text('个人信息'),
            trailing: const Icon(Icons.chevron_right),
            onTap: () {
              // TODO: 实现个人信息编辑页面
            },
          ),
          ListTile(
            leading: const Icon(Icons.location_on_outlined),
            title: const Text('收货地址'),
            trailing: const Icon(Icons.chevron_right),
            onTap: () {
              // TODO: 实现收货地址管理页面
            },
          ),
          ListTile(
            leading: const Icon(Icons.security),
            title: const Text('安全设置'),
            trailing: const Icon(Icons.chevron_right),
            onTap: () {
              // TODO: 实现安全设置页面
            },
          ),
          if (isAdmin)
            ListTile(
              leading: const Icon(Icons.admin_panel_settings),
              title: const Text('管理员面板'),
              trailing: const Icon(Icons.chevron_right),
              onTap: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => const AdminPanelScreen(),
                  ),
                );
              },
            ),
          ListTile(
            leading: const Icon(Icons.help_outline),
            title: const Text('帮助中心'),
            trailing: const Icon(Icons.chevron_right),
            onTap: () {
              // TODO: 实现帮助中心页面
            },
          ),
          ListTile(
            leading: const Icon(Icons.info_outline),
            title: const Text('关于我们'),
            trailing: const Icon(Icons.chevron_right),
            onTap: () {
              // TODO: 实现关于我们页面
            },
          ),
        ],
      ),
    );
  }
} 